package com.core.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.core.util.DspUtil;

public class LoginInterceptor implements HandlerInterceptor {
	Logger logger = Logger.getLogger(DspUtil.Log4J);

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		logger.info("afterHandle");
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		logger.info("postHandle");
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		// TODO Auto-generated method stub
		logger.info("preHandle");
		String url = request.getServletPath();
		String account = (String) request.getSession().getAttribute("user");

		if (!"/login".equals(url) && DspUtil.isEmpty(account)) {
			logger.info("no login the system, please login in.");
			return false;
		}
		logger.info("Welcome to system ," + account);
		return true;
	}

}
